package com.kldy8.kldyw.find.adapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.kldy8.kldyw.MyWebActivity;
import com.kldy8.kldyw.R;
import com.kldy8.kldyw.bean.New;
import com.kldy8.kldyw.web.AppWebUrl;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

/**
 * Created by user on 2016/12/15.
 */

public class NewInfoAdapter extends BaseAdapter
{
    ImageLoader loader;

    private List<New> listItems;
    private Context context;                        //运行上下文
    private LayoutInflater listContainer;           //视图容器
    private boolean[] hasChecked;

    //记录商品选中状态
    public final class ListItemView
    {                //自定义控件集合
        public ImageView image;
        public TextView title;
        public TextView info;
        public TextView text_from;
    }

    public void replaceOrAdd(boolean isInit, List<New> listItems)
    {
        if (isInit)
        {
            this.listItems = listItems;
        }
        else
        {
            if (this.listItems != null)
            {
                if (listItems != null)
                {
                    this.listItems.addAll(listItems);
                }
            }
            else
            {
                this.listItems = listItems;
            }
        }
    }

    public NewInfoAdapter(Context context,List<New> filmTalents)
    {
        this.context = context;
        this.listItems = filmTalents;
        listContainer = LayoutInflater.from(context);   //创建视图容器并设置上下文
        hasChecked = new boolean[getCount()];
        ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
                .build();
        loader = ImageLoader.getInstance();
        loader.init(config);
    }

    public int getCount()
    {
        // TODO Auto-generated method stub
        if (null == listItems )
        {
            return 1;
        }
        else
        {
            return listItems.size();
        }
    }

    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return arg0;
    }

    /**
     * 记录勾选了哪个物品
     * @param checkedID 选中的物品序号
     */
    private void checkedChange(int checkedID) {
        hasChecked[checkedID] = !hasChecked[checkedID];
    }

    /**
     * 判断物品是否选择
     * @param checkedID 物品序号
     * @return 返回是否选中状态
     */
    public boolean hasChecked(int checkedID) {
        return hasChecked[checkedID];
    }



    /**
     * ListView Item设置
     */
    public View getView(final int position, View convertView, ViewGroup parent)
    {

        // TODO Auto-generated method stub
        final int selectID = position;
        //自定义视图
        ListItemView listItemView = null;
        if (null == listItems || listItems.size() == 0)
        {
            convertView = listContainer.inflate(R.layout.view_notfount, null);
            return convertView;
        }
        else{
            if (convertView == null)
            {
                listItemView = new ListItemView();
                //获取list_item布局文件的视图
                convertView = listContainer.inflate(R.layout.new_info_item, null);
                //获取控件对象
                listItemView.image = (ImageView) convertView.findViewById(R.id.image);
                listItemView.title = (TextView)convertView.findViewById(R.id.title);
                listItemView.info = (TextView)convertView.findViewById(R.id.supportingText);
                listItemView.text_from= (TextView)convertView.findViewById(R.id.text_from);
                //设置控件集到convertView
                convertView.setTag(listItemView);
            }
            else
            {
                listItemView = (ListItemView)convertView.getTag();
            }

            New  new_item = listItems.get(position);
            String headUrl = new_item.cover_pic;
            loader.displayImage(headUrl, listItemView.image);
            listItemView.title.setText(new_item.title);
//            listItemView.label.setText(filmTalent.position);
//            listItemView.label.setBackgroundColor(LabelColor.chooseColor(context,filmTalent.position));
            listItemView.info.setText(new_item.summary);
//            listItemView.text_from.setText(filmTalent.areaname);


            convertView.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v) {
                    New  new_item = listItems.get(position);
                    Intent intent = new Intent();
                    String resultUrl = AppWebUrl.createUrl(new_item.news_url);
                    intent.putExtra("url",resultUrl);
                    intent.setClass(context, MyWebActivity.class);
                    ((Activity)context).startActivity(intent);
                    ((Activity)context).overridePendingTransition(R.anim.slide_in_right,R.anim.slide_out_left);
                }
            });

            return convertView;
        }
    }
}
